import java.util.*;

/**
 * 走迷宫：BFS
 * https://www.nowcoder.com/practice/e88b41dc6e764b2893bc4221777ffe64?tpId=308&tqId=40477&ru=/exam/oj
 */
public class Main {
    static int[] dx = {0, 0, 1, -1};
    static int[] dy = {-1, 1, 0, 0};
    static int n, m;
    static int[][] dist = new int[1010][1010];

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        n = in.nextInt();
        m = in.nextInt();
        in.nextLine();
        int x1 = in.nextInt(), y1 = in.nextInt(), x2 = in.nextInt(), y2 = in.nextInt();
        in.nextLine();
        char g[][] = new char[n + 1][m + 1];

        for (int i = 1; i <= n; i++) {
            String s = in.nextLine();
            for (int j = 1; j <= m; j++) {
                g[i][j] = s.charAt(j - 1);
            }
        }

        for (int i = 0; i < 1010; i++)
            for (int j = 0; j < 1010; j++)
                dist[i][j] = -1;

        int ret = bfs(g, x1, y1, x2, y2);

        System.out.println(ret);
    }

    public static int bfs(char[][] g, int x1, int y1, int x2, int y2) {

        if (g[x1][y1] == '*') return -1;

        //创建一个队列
        Queue<int[]> q = new LinkedList<>();
        q.offer(new int[] {x1, y1});
        dist[x1][y1] = 0;
        while (!q.isEmpty()) {
            int[] tem = q.poll();

            int a = tem[0];
            int b = tem[1];

            for (int k = 0; k < 4; k++) {
                int x = dx[k] + a;
                int y = dy[k] + b;

                if (x >= 1 && x <= n && y >= 1 && y <= m && dist[x][y] == -1 && g[x][y] != '*') {
                    if(x == x2 && y == y2) return dist[a][b] + 1;
                    dist[x][y] = dist[a][b] + 1;
                    q.offer(new int[]{x,y});
                }
            }
        }

        return -1;
    }
}

/**
 * NC50039：kotori和气球
 * https://ac.nowcoder.com/acm/problem/50039
 */
class Main2{
    public static void main(String[] args){
        Scanner in = new Scanner(System.in);

        int n = in.nextInt(),m = in.nextInt();

        //数学排序组合
        int num = n;
        for(int i = 0;i < m - 1;i++){
            num = (num * (n - 1)) % 109;
        }

        System.out.println(num);

        PriorityQueue q = new PriorityQueue<>();
        q.poll();
    }
}


class Solution {

    public static int minmumNumberOfHost (int n, int[][] startEnd)
    {
        //1,排序
        Arrays.sort(startEnd,(a,b) -> {
           //return a[0] - b[0];
            //return a[0] <= b[0] ? -1 : 1;

            if(a[0] >= b[0]) return 1;
            else return -1;
        });

        //创建优先级队列：元素右端点
        PriorityQueue<Integer> q = new PriorityQueue<>();

        q.add(startEnd[0][1]);

        //比较
        for(int i = 1;i < n;i++){

            //左端点大于堆顶元素
            int t = q.peek();
            if(t <= startEnd[i][0]){
                //更新右端点
                q.offer(startEnd[i][1]);
                q.poll();
            }
            //新的端点入对
            else{
                q.offer(startEnd[i][1]);
            }
        }

        return q.size();
    }

    public static void main(String[] args) {
//        int[][] d = new int[][]{{2147483646,2147483647},{-2147483648,-2147483647},
//                {2147483646,2147483647},{-2147483648,-2147483647},{2147483646,2147483647},
//                {-2147483648,-2147483647},{2147483646,2147483647},{-2147483648,-2147483647},
//        {2147483646,2147483647},{-2147483648,-2147483647}};
        int[][] d = new int[][]{{2147483646,2147483647},{-2147483648,-2147483647},};
        Solution.minmumNumberOfHost(2,d);
    }
}